package cn.edu.sgu.www.mhxysy.service.chongwu_equipment.impl;

import cn.edu.sgu.www.mhxysy.pager.Pager;
import cn.edu.sgu.www.mhxysy.dto.chongwu_equipment.ChongwuWaiguanDTO;
import cn.edu.sgu.www.mhxysy.entity.chongwu_equipment.ChongwuWaiguanCategory;
import cn.edu.sgu.www.mhxysy.exception.GlobalException;
import cn.edu.sgu.www.mhxysy.mapper.chongwu_equipment.ChongwuWaiguanCategoryMapper;
import cn.edu.sgu.www.mhxysy.pager.chongwu_equipment.ChongwuWaiguanPager;
import cn.edu.sgu.www.mhxysy.restful.ResponseCode;
import cn.edu.sgu.www.mhxysy.service.chongwu_equipment.ChongwuWaiguanCategoryService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 沐雨橙风ιε
 * @version 1.0
 */
@Service
public class ChongwuWaiguanCategoryServiceImpl implements ChongwuWaiguanCategoryService {

	private final ChongwuWaiguanCategoryMapper chongwuWaiguanCategoryMapper;

	@Autowired
	public ChongwuWaiguanCategoryServiceImpl(ChongwuWaiguanCategoryMapper chongwuWaiguanCategoryMapper) {
		this.chongwuWaiguanCategoryMapper = chongwuWaiguanCategoryMapper;
	}

	@Override
	public void insert(ChongwuWaiguanDTO chongwuWaiguanDTO) {
		ChongwuWaiguanCategory chongwuWaiguanCategory = toEntity(chongwuWaiguanDTO);

		chongwuWaiguanCategory.setId(null);

		chongwuWaiguanCategoryMapper.insert(chongwuWaiguanCategory);
	}

	@Override
	public void updateById(ChongwuWaiguanDTO chongwuWaiguanDTO) {
		if (chongwuWaiguanDTO.getId() == null) {
			throw new GlobalException(ResponseCode.BAD_REQUEST, "外观ID不能为空！");
		}

		ChongwuWaiguanCategory chongwuWaiguanCategory = toEntity(chongwuWaiguanDTO);

		chongwuWaiguanCategoryMapper.updateById(chongwuWaiguanCategory);
	}

	@Override
	public List<ChongwuWaiguanCategory> selectAll() {
		return chongwuWaiguanCategoryMapper.selectList(null);
	}

	@Override
	public ChongwuWaiguanCategory selectById(Integer id) {
		return chongwuWaiguanCategoryMapper.selectById(id);
	}

	@Override
	public List<ChongwuWaiguanCategory> selectList(Integer id) {
		QueryWrapper<ChongwuWaiguanCategory> queryWrapper = new QueryWrapper<>();

		if (id != null) {
			queryWrapper.eq("id", id);
			queryWrapper.or();
			queryWrapper.eq("id", 0);
		}
		
		return chongwuWaiguanCategoryMapper.selectList(queryWrapper);
	}

	@Override
	public Page<ChongwuWaiguanCategory> selectByPage(ChongwuWaiguanPager pager) {
		QueryWrapper<ChongwuWaiguanCategory> queryWrapper = new QueryWrapper<>();
		Page<ChongwuWaiguanCategory> page = Pager.ofPage(pager);

		queryWrapper.eq(
				pager.getPropertyId() != null,
				"property_id", pager.getPropertyId()
		);

		return chongwuWaiguanCategoryMapper.selectPage(page, queryWrapper);
	}

	private ChongwuWaiguanCategory toEntity(ChongwuWaiguanDTO chongwuWaiguanDTO) {
		Integer id = chongwuWaiguanDTO.getId();
		String name = chongwuWaiguanDTO.getName();

		// 验证外观名称是否重复
		QueryWrapper<ChongwuWaiguanCategory> queryWrapper = new QueryWrapper<>();

		queryWrapper.ne(id != null, "id", id);
		queryWrapper.eq("name", name);

		Long count = chongwuWaiguanCategoryMapper.selectCount(queryWrapper);

		if (count > 0) {
			throw new GlobalException(ResponseCode.CONFLICT, "外观名称重复！");
		}

		ChongwuWaiguanCategory chongwuWaiguanCategory = new ChongwuWaiguanCategory();

		chongwuWaiguanCategory.setId(id);
		chongwuWaiguanCategory.setName(name);
		chongwuWaiguanCategory.setNote(chongwuWaiguanDTO.getNote());
		chongwuWaiguanCategory.setPropertyId(chongwuWaiguanDTO.getPropertyId());
		chongwuWaiguanCategory.setPropertyValue(chongwuWaiguanDTO.getPropertyValue());

		return chongwuWaiguanCategory;
	}

}